package com.sgb.kjwl.view.ui.activity.transport.driver.goods;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.tabs.TabLayout;
import com.sgb.capital.callback.Constants;
import com.sgb.capital.callback.ZMessageEvent;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.view.ui.activity.transport.CargoQuotationActivity;
import com.swgk.core.event.MessageEvent;
import com.sgb.kjwl.databinding.ActivityGoodsGeneralTransportationDetailBinding;
import com.sgb.kjwl.model.entity.self.GoodsSourceDetail;
import com.sgb.kjwl.model.entity.self.UserAccount;
import com.sgb.kjwl.model.entity.transport.GoodsSourcePlace;
import com.sgb.kjwl.utils.CargoConstant;
import com.sgb.kjwl.utils.EntityConversion;
import com.sgb.kjwl.utils.LogisticsPermissionHelper;
import com.sgb.kjwl.utils.NumberUtil;
import com.swgk.core.util.MLog;
import com.swgk.core.util.UserOperating;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LogisticsEvent;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteInfoActivity;
import com.sgb.kjwl.view.ui.activity.self.logistics.OfferListGeneralGoodsActivity;
import com.sgb.kjwl.view.ui.activity.self.logistics.OfferOwnerActivity;
import com.sgb.kjwl.view.ui.activity.self.logistics.OfferThirdActivity;
import com.sgb.kjwl.view.ui.activity.self.logistics.PublishCargoActivity;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;
import com.sgb.kjwl.view.ui.activity.transport.HandleAdressActivity;
import com.sgb.kjwl.view.ui.activity.transport.InquireByLetterActvity;
import com.sgb.kjwl.view.ui.activity.transport.PayGoodsOrderActivity;
import com.sgb.kjwl.view.ui.activity.transport.ShareLogisticsActivity;
import com.sgb.kjwl.view.ui.adapter.transport.GeneralPrjDetailAdapter;
import com.sgb.kjwl.view.widget.RoundImageView;
import com.sgb.link.entity.LogisticsFlowLog;
import com.sgb.link.utils.LogisticsLogUtils;
import com.swgk.core.base.aroute.AppRouteConstant;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

/**
 * @Author : 熊丽
 * @date :2021/2/26 0026$ 18:12$
 * @Description :普通货运项目$
 */
@Route(path = AppRouteConstant.WuLui.ROUTER_PATH_GENERALPRJDETAILACTIVITY)
public class GeneralPrjDetailActivity extends AppActivity implements GeneralPrjDetailAdapter.ItemOnClickListen {
    private ActivityGoodsGeneralTransportationDetailBinding mBinding;
    private String[] titles = {"货运信息", "车辆要求", "货运要求", "付款约定", "竞价单位"};
    private String[] personTitles = {"货运信息", "车辆要求", "货运要求", "付款约定"};
    private int selectPos = 0, mlobbyselect = 0;
    private boolean mshipperManagement = false;
    GeneralPrjDetailAdapter mAdapter;
    private List<GoodsSourceDetail> getManageItemData = new ArrayList<>();
    private String sourceNo;
    private Timer timer;
    private GoodsSourceDetail mGoodsSourceDetail = null;
    private boolean isFirst = false;
    GoodsDetailModel mModel;
    String company, orderNo;
    LogisticsFlowLog flowLog;
    String pageNameType;

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(GeneralPrjDetailActivity.this, R.layout.activity_goods_general_transportation_detail);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        mModel = new ViewModelProvider(this).get(GoodsDetailModel.class);
        initEvent();
        initData();
    }

    private void initData() {
        mModel.upAndDownGoods.observe(this, list -> {
            downAndUpGoods();
        });
        mModel.deleteInfoSuccess.observe(this, list -> {
            deleteInfoSuccess();
        });
        mModel.confirmFundAccountData.observe(this, list -> {
            getGoodsOwnerInfo(list);
        });
        mModel.GoodsSourceDetailDataEmpty.observe(this, list -> {
            showEmptyView(list);
        });
        mModel.setUserInfo.observe(this, userData -> {
            LogisticsPermissionHelper.getInstance().checkPermissionToOrder(this, mGoodsSourceDetail.getNo(), mGoodsSourceDetail.getTerminal(), mGoodsSourceDetail.getMessagePrice());
        });
        // mModel.checkCarInfo.observe(this,this::setcheckCarInfo);
    }

    //goodstype竞价1和普通0   position大厅false和个人中心ture
    public static void start(Context context, String sourceNo, int goodstype, boolean position) {
        Intent intent = new Intent();
        intent.putExtra("sourceNo", sourceNo);
        intent.putExtra("goodsType", goodstype);
        intent.putExtra("position", position);
        intent.setClass(context, GeneralPrjDetailActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    private void initEvent() {
        sourceNo = getIntent().getStringExtra("sourceNo");
        mlobbyselect = getIntent().getIntExtra("goodsType", 0);
        mshipperManagement = getIntent().getBooleanExtra("position", false);
        Request();
        mBinding.topLayout.ivBack.setOnClickListener(v -> finish());
        mBinding.onTheShelf.setOnClickListener(this);
        mBinding.editing.setOnClickListener(this);
        mBinding.delete.setOnClickListener(this);
        mBinding.tvReport.setOnClickListener(this);
        mBinding.tvContactService.setOnClickListener(this);
        mBinding.tvInquireByLetter.setOnClickListener(this);
        mBinding.tvCommit.setOnClickListener(this);
        mBinding.ggtDituIcon.setOnClickListener(this);
        mBinding.consignorGgtDituIcon.setOnClickListener(this);
        mBinding.offerNum.setOnClickListener(this);
        mBinding.headeCompay.callPhone.setOnClickListener(this);
        mBinding.bottomCompay.callPhone.setOnClickListener(this);
        mBinding.carRl.setOnClickListener(this);
        mBinding.tvInvitation.setOnClickListener(this);
        mBinding.tvTakeOrders.setOnClickListener(this);
        mBinding.managerStatusFigureOperation.setOnClickListener(this);
        mBinding.tvFigureOperation.setOnClickListener(this);
        mBinding.quotationReason.setOnClickListener(this);
        if (!mshipperManagement) {
            mBinding.ownerPhoneValue.setOnClickListener(this);
        }
        mBinding.loadPhoneValue.setOnClickListener(this);
        mBinding.unloadPhoneValue.setOnClickListener(this);
        if (mshipperManagement) {
            mBinding.topLayout.shareImageView.setVisibility(View.GONE);
        } else {
            mBinding.topLayout.shareImageView.setVisibility(View.VISIBLE);
            mBinding.topLayout.shareImageView.setOnClickListener(this);
        }
        if (mlobbyselect == 1) {
            mBinding.topLayout.titleView.setText("货源项目");
        } else {
            if (mshipperManagement) {
                mBinding.topLayout.titleView.setText("货源项目");
            }
            mBinding.topLayout.titleView.setText("货源项目");
        }
        mBinding.headeCompay.callPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isLogin()) {
                    VerificationCodeLoginActivity.start(GeneralPrjDetailActivity.this);
                    return;
                }
                flowLog=new LogisticsFlowLog(pageNameType,"货源联系人","icon");
                showConsignorDialog(mGoodsSourceDetail.getConnectionPhone());
            }
        });
        mBinding.bottomCompay.callPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isLogin()) {
                    VerificationCodeLoginActivity.start(GeneralPrjDetailActivity.this);
                    return;
                }
                flowLog=new LogisticsFlowLog(pageNameType,"货源联系人","icon");
                showConsignorDialog(mGoodsSourceDetail.getConnectionPhone());
            }
        });
        mBinding.headeCompay.callPhonePerson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isLogin()) {
                    VerificationCodeLoginActivity.start(GeneralPrjDetailActivity.this);
                    return;
                }
                flowLog=new LogisticsFlowLog(pageNameType,"货源联系人","icon");
                showConsignorDialog(mGoodsSourceDetail.getConnectionPhone());
            }
        });
    }

    private View.OnClickListener mTabOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectPos = (int) view.getTag();
            mBinding.tablayout.setTag(selectPos);
            TabLayout.Tab tab = mBinding.tablayout.getTabAt(selectPos);
            if (tab != null) {
                tab.select();
                getManageItemData.get(0).setItemType(selectPos);
                mAdapter.setNewData(getManageItemData);
                mAdapter.notifyDataSetChanged();
            }
        }
    };

    /**
     * 初始化 底部布局
     *
     * @param
     */
    private void initTabView() {
        if (mBinding.tablayout.getTabCount() > 0) {
            mBinding.tablayout.removeAllTabs();
        }
        if ((UserOperating.getInstance().isCompany() && UserOperating.getInstance().getTeamOrCompanyNameNo().equals(mGoodsSourceDetail.getCompanyNo()) && mGoodsSourceDetail.getTerminal() == 1)
                || (UserOperating.getInstance().isTeam() && UserOperating.getInstance().getTeamOrCompanyNameNo().equals(mGoodsSourceDetail.getTeamNo()) && mGoodsSourceDetail.getTerminal() == 2)
                || (UserOperating.getInstance().isPersonal() && UserOperating.getInstance().getUserNo().equals(mGoodsSourceDetail.getUserNo()) && mGoodsSourceDetail.getTerminal() == 3)) {
            for (String title : mshipperManagement ? personTitles : titles) {
                mBinding.tablayout.addTab(mBinding.tablayout.newTab().setText(title));
            }

        } else {
            for (String title : personTitles) {
                mBinding.tablayout.addTab(mBinding.tablayout.newTab().setText(title));
            }
        }

        mBinding.tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                mBinding.tablayout.setTag(tab.getTag());
                if (getManageItemData != null) {
                    getManageItemData.get(0).setItemType(tab.getPosition());
                }
                mAdapter.setNewData(getManageItemData);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    public void setDetail(GoodsSourceDetail manageItemData) {
        if(manageItemData.getPattern()==0){//埋点路径v2.2.3普通和竞价同一页面 3-普通货源详情  4-竞价货源详情
            pageNameType="3";
        }else{
            pageNameType="4";
        }
        mBinding.contnetLayout.setVisibility(View.VISIBLE);
        mBinding.tablayout.selectTab(mBinding.tablayout.getTabAt(0));
        if (manageItemData == null)
            return;
        if (getManageItemData == null) {
            getManageItemData = new ArrayList<>();
            getManageItemData.add(manageItemData);
        } else {
            getManageItemData.clear();
            getManageItemData.add(manageItemData);
            if (mlobbyselect == 1) {
                getManageItemData.get(0).setBidding(true);
            } else {
                getManageItemData.get(0).setBidding(false);
            }
        }
        mBinding.tvTag.setVisibility(manageItemData.getPattern()!=null&&manageItemData.getPattern()==1?View.VISIBLE:View.GONE);
        mGoodsSourceDetail = manageItemData;
        mAdapter = new GeneralPrjDetailAdapter(this, getManageItemData, mshipperManagement);
        mBinding.recyclerViewContent.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBinding.recyclerViewContent.setAdapter(mAdapter);
        mAdapter.SetItemOnClickListen(this);
        setShow();
        setStateInfo(manageItemData);
        setCompanyInfo(manageItemData);
        fieldDisplay(manageItemData);
        setGoodsInfo(manageItemData);
        setSendReciveInfo(manageItemData);
        setbottomCompanyInfo(manageItemData);
        setlowestPrice(manageItemData);
        //判断是否货主身份
        if ((UserOperating.getInstance().isCompany() && UserOperating.getInstance().getTeamOrCompanyNameNo().equals(mGoodsSourceDetail.getCompanyNo()) && mGoodsSourceDetail.getTerminal() == 1)
                || (UserOperating.getInstance().isTeam() && UserOperating.getInstance().getTeamOrCompanyNameNo().equals(mGoodsSourceDetail.getTeamNo()) && mGoodsSourceDetail.getTerminal() == 2)
                || (UserOperating.getInstance().isPersonal() && UserOperating.getInstance().getUserNo().equals(mGoodsSourceDetail.getUserNo()) && mGoodsSourceDetail.getTerminal() == 3)) {
            mBinding.tvFigureOperation.setVisibility(View.VISIBLE);
            mBinding.managerStatusFigureOperation.setVisibility(View.VISIBLE);
            mBinding.quotationReason.setVisibility(View.VISIBLE);

        } else {
            mBinding.quotationReason.setVisibility(View.INVISIBLE);
            mBinding.tvFigureOperation.setVisibility(View.GONE);
            mBinding.managerStatusFigureOperation.setVisibility(mshipperManagement ? View.VISIBLE : View.GONE);
        }
        mBinding.splin4Line.setVisibility(mshipperManagement ? View.GONE : View.VISIBLE);
        initTabView();
    }

    public void showEmptyView(String msg) {
        mBinding.noGoods.setVisibility(View.VISIBLE);
        mBinding.contnetLayout.setVisibility(View.GONE);
        mBinding.bottomLayout.setVisibility(View.GONE);
        mBinding.goodsTips.setText(msg);
    }

    //设置企业信息
    private void setCompanyInfo(GoodsSourceDetail manageItemData) {
        mBinding.headeCompay.callPhonePerson.setVisibility(View.GONE);
        mBinding.headeCompay.companyName.setText(manageItemData.getCompanyName());
        mBinding.headeCompay.businessAddressText.setText(manageItemData.getCompanyAddress());
        mBinding.headeCompay.enterpriseContactName.setText(manageItemData.getConnection());
        mBinding.headeCompay.enterprisePhoneNumber.setText(manageItemData.getConnectionPhone());
        mBinding.headeCompay.enterprisePhoneNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isLogin()) {
                    VerificationCodeLoginActivity.start(GeneralPrjDetailActivity.this);
                    return;
                }
                flowLog=new LogisticsFlowLog(pageNameType,"货源联系人","icon");
                showConsignorDialog(manageItemData.getConnectionPhone());
            }
        });
        mBinding.headeCompay.rlInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(manageItemData.getCompanyNo()) && manageItemData.getTerminal() != null && manageItemData.getTerminal() == 1) {
                    WebsiteInfoActivity.start(GeneralPrjDetailActivity.this, mGoodsSourceDetail.getCompanyNo(), WebsiteInfoActivity.WEBSITE_FOR_COMPANY);
                } else if (!TextUtils.isEmpty(manageItemData.getTeamNo()) && manageItemData.getTerminal() != null && manageItemData.getTerminal() == 2) {
                    WebsiteInfoActivity.start(GeneralPrjDetailActivity.this, mGoodsSourceDetail.getTeamNo(), WebsiteInfoActivity.WEBSITE_FOR_TEAM);
                } else {
                    WebsiteInfoActivity.start(GeneralPrjDetailActivity.this, mGoodsSourceDetail.getUserNo(), WebsiteInfoActivity.WEBSITE_FOR_PERSONAL);
                }
            }
        });
        RequestOptions options = new RequestOptions()
                .dontAnimate()
                .placeholder(manageItemData.getTerminal() != null ? manageItemData.getTerminal() == 3 ? R.drawable.geren_person_touxiang : manageItemData.getTerminal() == 1 ? R.mipmap.goods_list_company_icon : R.drawable.goods_detail_touxiang : R.drawable.goods_detail_touxiang)
                .error(manageItemData.getTerminal() != null ? manageItemData.getTerminal() == 3 ? R.drawable.geren_person_touxiang : manageItemData.getTerminal() == 1 ? R.mipmap.goods_list_company_icon : R.drawable.goods_detail_touxiang : R.drawable.goods_detail_touxiang)
                .centerCrop();
        if (!isDestroyed()) {
            switch (manageItemData.getTerminal()) {
                case 1:
                    mBinding.headeCompay.imgLogo.setOval(false);
                    mBinding.headeCompay.imgLogo.setCornerRadius(4);
                    break;
                case 2:
                    //个人不展示
                case 3:
                    mBinding.headeCompay.imgLogo.setOval(true);
                    break;
            }
            //  mBinding.headeCompay.imgLogo.setType(manageItemData.getTerminal() != null && manageItemData.getTerminal() == 1 ? RoundImageView.TYPE_ROUND : RoundImageView.TYPE_CIRCLE);
            Glide.with(this).load(manageItemData.getLogo()).apply(options).into(mBinding.headeCompay.imgLogo);
        }
        if (manageItemData.getTerminal() == null) {
            mBinding.headeCompay.role.setBackgroundResource(R.drawable.bg_blue_solid_ring);
            mBinding.headeCompay.role.setTextColor(Color.parseColor("#5792FD"));
            mBinding.headeCompay.role.setText("企业");
            return;
        } else {
            if (manageItemData.getTerminal() == 1) {
                mBinding.headeCompay.role.setBackgroundResource(R.drawable.bg_blue_solid_ring);
                mBinding.headeCompay.role.setTextColor(Color.parseColor("#5792FD"));
                mBinding.headeCompay.role.setText("企业");
            } else if (manageItemData.getTerminal() == 2) {
                mBinding.headeCompay.role.setText("中介");
                mBinding.headeCompay.role.setTextColor(Color.parseColor("#06C764"));
            } else if (manageItemData.getTerminal() == 3) {
                mBinding.headeCompay.callPhonePerson.setVisibility(View.VISIBLE);
                mBinding.headeCompay.personRole.setVisibility(View.GONE);
                mBinding.headeCompay.companyContacks.setVisibility(View.GONE);
                mBinding.headeCompay.role.setBackgroundResource(R.drawable.bg_orange_ring);
                mBinding.headeCompay.personRole.setText("个人");
                mBinding.headeCompay.personRole.setTextColor(Color.parseColor("#FFC000"));
                mBinding.headeCompay.businessAddress.setText("联系电话：");
                mBinding.headeCompay.businessAddressText.setText(manageItemData.getConnectionPhone());
                mBinding.headeCompay.businessAddressText.setTextColor(getResources().getColor(R.color.c_5792FD));
                mBinding.headeCompay.businessAddressText.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!isLogin()) {
                            VerificationCodeLoginActivity.start(GeneralPrjDetailActivity.this);
                            return;
                        }
                        flowLog=new LogisticsFlowLog(pageNameType,"货源联系人","icon");
                        showConsignorDialog(manageItemData.getConnectionPhone());
                    }
                });
            } else {
                mBinding.headeCompay.role.setVisibility(View.GONE);
            }
        }
    }

    private void setlowestPrice(GoodsSourceDetail manageItemData) {
        if (!mshipperManagement) {
            if (manageItemData != null && manageItemData.getIntentionCarNumber() == null || manageItemData.getIntentionCarNumber() == 0) {
                mBinding.cutoffTime.setVisibility(View.GONE);
            } else {
                mBinding.cutoffTime.setVisibility(View.VISIBLE);
            }
            mBinding.intentionalCar.setText(Html.fromHtml("<font size='12px'>意向人数：</font>" + ("<font color='#666666' size='20px'>" + NumberUtil.trim0(manageItemData.getIntentionCarNumber()) + "" + "</font>")));
            mBinding.lowestQuotationText.setText((NumberUtil.trim0(manageItemData.getLowestPrice()) + "元"));
            mBinding.lowestQuotationLl.setVisibility(View.VISIBLE);
        } else {
            mBinding.cutoffTime.setVisibility(View.GONE);
        }
    }

    private void setGoodsInfo(GoodsSourceDetail manageItemData) {
        StringBuilder goodType = new StringBuilder();
        if (!TextUtils.isEmpty(manageItemData.getGoodsTypeName())) {
            goodType.append(manageItemData.getGoodsTypeName());
        }
        if (!TextUtils.isEmpty(manageItemData.getSecondGoodsTypeName())) {
            goodType.append(TextUtils.isEmpty(manageItemData.getGoodsTypeName()) ? manageItemData.getSecondGoodsTypeName() : "/" + manageItemData.getSecondGoodsTypeName());
        }

        mBinding.gtdGoodsNameText.setText(TextUtils.isEmpty(manageItemData.getGoodsTypeName()) && TextUtils.isEmpty(manageItemData.getSecondGoodsTypeName()) ? "/" : goodType);
        if (TextUtils.isEmpty(manageItemData.getWeight())) {
            if (!TextUtils.isEmpty(manageItemData.getVolume())) {
                mBinding.gtdFreightVolumeText.setText(NumberUtil.trim0(manageItemData.getVolume()) + "方");
            } else {
                mBinding.gtdFreightVolumeText.setText("/");
            }
        } else {
            mBinding.gtdFreightVolumeText.setText(NumberUtil.trim0(manageItemData.getWeight()) + "吨" + (TextUtils.isEmpty(manageItemData.getVolume()) ? "" : "  " + NumberUtil.trim0(manageItemData.getVolume()) + "方"));
        }
        mBinding.gtdFreightUnitPrice.setVisibility(manageItemData.getFreightType() == null || manageItemData.getFreightType() == 0 ? View.VISIBLE : View.GONE);
        mBinding.gtdFreightUnitPriceText.setVisibility(manageItemData.getFreightType() == null || manageItemData.getFreightType() == 0 ? View.VISIBLE : View.GONE);
        mBinding.gtdFreightUnitPriceText.setText(NumberUtil.trim0(manageItemData.getFreight()) + manageItemData.getFreightUnitType());
        mBinding.gtdInvoiceRequirementText.setText(TextUtils.isEmpty(manageItemData.getInvoiceWords()) ? "/" : manageItemData.getInvoiceWords().equals("不开发票") ? manageItemData.getInvoiceWords() :
                NumberUtil.trim0(manageItemData.getTariff()) + "%" + manageItemData.getInvoiceWords());
        mBinding.gtdPaymentMethodText.setText(TextUtils.isEmpty(manageItemData.getPayType()) ? "/" : manageItemData.getPayType());


        StringBuilder builder = new StringBuilder();
        if (!TextUtils.isEmpty(manageItemData.getPrepaidProportion()) && !manageItemData.getPrepaidProportion().equals("0")) {
            builder.append("预付款").append(manageItemData.getPrepaidProportion()).append("%，");
        }
        if (manageItemData.getPaymentAfterLoading() != 0) {
            builder.append("装车后支付").append(manageItemData.getPaymentAfterLoading()).append("%，");
        }
        if (manageItemData.getArrivalAcceptancePayment() != 0) {
            builder.append("到货验收后支付").append(manageItemData.getArrivalAcceptancePayment()).append("%（尾款由验收人验收后支付），");
        }
        if (builder.length() > 1) {
            mBinding.gtdPaymentMethodTextAx.setText(builder.substring(0, builder.length() - 1));
            mBinding.gtdPaymentMethodTextAx.setVisibility(View.VISIBLE);
        } else {
            mBinding.gtdPaymentMethodTextAx.setVisibility(View.GONE);
        }
        mBinding.gtdReceivingRl.setVisibility(TextUtils.isEmpty(manageItemData.getDestDetailAddress()) ? View.GONE : View.VISIBLE);
        mBinding.gtdConsignorRl.setVisibility(TextUtils.isEmpty(manageItemData.getDetailAddress()) ? View.GONE : View.VISIBLE);
        mBinding.gtdConsignorAddressText.setText(TextUtils.isEmpty(manageItemData.getDetailAddress()) ? "/" : manageItemData.getProvinceName() + manageItemData.getCityName() + manageItemData.getDistrictName() + manageItemData.getDetailAddress());
        mBinding.gtdReceivingAddressText.setText(TextUtils.isEmpty(manageItemData.getDestDetailAddress()) ? "/" : manageItemData.getDestProvinceName() + manageItemData.getDestCityName() + manageItemData.getDestDistrictName() + manageItemData.getDestDetailAddress());
    }

    //发货人收货人信息
    private void setSendReciveInfo(GoodsSourceDetail manageItemData) {
//        mBinding.ditSendInfo.setContent1(TextUtils.isEmpty(manageItemData.getSendPhone()) ? "/" : mshipperManagement ? manageItemData.getSendPhone() : StringUtil.getPhoneToName(manageItemData.getSendPhone()));
//        mBinding.ditSendInfo.setContent2(TextUtils.isEmpty(manageItemData.getSendGoodsUnit()) ? "/" : manageItemData.getSendGoodsUnit());
//        mBinding.ditSendInfo.setInfoContent(TextUtils.isEmpty(manageItemData.getSendPeople()) ? "/" : mshipperManagement ? manageItemData.getSendPeople() : StringUtil.left(manageItemData.getSendPeople(), 1));
//        mBinding.ditResiveInfo.setContent1(TextUtils.isEmpty(manageItemData.getReceptionPhone()) ? "/" : mshipperManagement ? manageItemData.getReceptionPhone() : StringUtil.getPhoneToName(manageItemData.getReceptionPhone()));
//        mBinding.ditResiveInfo.setContent2(TextUtils.isEmpty(manageItemData.getReceiveGoodsUnit()) ? "/" : manageItemData.getReceiveGoodsUnit());
//        mBinding.ditResiveInfo.setInfoContent(TextUtils.isEmpty(manageItemData.getReceptionPeople()) ? "/" : mshipperManagement ? manageItemData.getReceptionPeople() : StringUtil.left(manageItemData.getReceptionPeople(), 1));
//        mBinding.ditSendInfo.setVisiabelRlOne(TextUtils.isEmpty(manageItemData.getSendPeople()) ? true : false, TextUtils.isEmpty(manageItemData.getSendPhone()) ? true : false, TextUtils.isEmpty(manageItemData.getSendGoodsUnit()) ? true : false);
//        mBinding.ditResiveInfo.setVisiabelRlOne(TextUtils.isEmpty(manageItemData.getReceptionPeople()) ? true : false, TextUtils.isEmpty(manageItemData.getReceptionPhone()) ? true : false, TextUtils.isEmpty(manageItemData.getReceiveGoodsUnit()) ? true : false);
//
        if (TextUtils.isEmpty(manageItemData.getSendPeople()) && TextUtils.isEmpty(manageItemData.getSendPhone())) {
            mBinding.loadPeopleValue.setText("/");
        } else {
            mBinding.loadPeopleValue.setText(manageItemData.getSendPeople());
            mBinding.loadPhoneValue.setText(manageItemData.getSendPhone());
        }
        if (TextUtils.isEmpty(manageItemData.getReceptionPeople()) && TextUtils.isEmpty(manageItemData.getReceptionPhone())) {
            mBinding.unloadPeopleValue.setText("/");
        } else {
            mBinding.unloadPeopleValue.setText(manageItemData.getReceptionPeople());
            mBinding.unloadPhoneValue.setText(manageItemData.getReceptionPhone());
        }

        if (TextUtils.isEmpty(mGoodsSourceDetail.getConnection()) && TextUtils.isEmpty(mGoodsSourceDetail.getConnectionPhone())) {
            mBinding.ownerPeopleValue.setText("/");
        } else {
            mBinding.ownerPeopleValue.setText(!TextUtils.isEmpty(mGoodsSourceDetail.getConnection()) ? mGoodsSourceDetail.getConnection() : "");
            mBinding.ownerPhoneValue.setText(!TextUtils.isEmpty(mGoodsSourceDetail.getConnectionPhone()) ? mGoodsSourceDetail.getConnectionPhone() : "");
        }

        if (TextUtils.isEmpty(mGoodsSourceDetail.getConnectionPhone())) {
            mBinding.ownerPhoneValue.setCompoundDrawables(null, null, null, null);
        }

        if (TextUtils.isEmpty(manageItemData.getSendPhone())) {
            mBinding.loadPhoneValue.setCompoundDrawables(null, null, null, null);
        }
        if (TextUtils.isEmpty(manageItemData.getReceptionPhone())) {
            mBinding.unloadPhoneValue.setCompoundDrawables(null, null, null, null);
        }

        if (mshipperManagement) {
            mBinding.loadPhoneValue.setCompoundDrawables(null, null, null, null);
            mBinding.unloadPhoneValue.setCompoundDrawables(null, null, null, null);
            mBinding.ownerPhoneValue.setCompoundDrawables(null, null, null, null);
            mBinding.loadPhoneValue.setTextColor(getResources().getColor(R.color.FF333333));
            mBinding.ownerPhoneValue.setTextColor(getResources().getColor(R.color.FF333333));
            mBinding.unloadPhoneValue.setTextColor(getResources().getColor(R.color.FF333333));
        }

        if (TextUtils.isEmpty(manageItemData.getPerformanceBond())) {
            mBinding.securityDepositText.setTextColor(getResources().getColor(R.color.color_333333));
            mBinding.securityDepositText.setText("/");
        } else {
            if ("0".equals(NumberUtil.trim0(manageItemData.getPerformanceBond()))) {
                mBinding.securityDepositText.setTextColor(getResources().getColor(R.color.color_333333));
            }
            mBinding.securityDepositText.setText("0".equals(NumberUtil.trim0(manageItemData.getPerformanceBond())) ? "/" : NumberUtil.trim0(manageItemData.getPerformanceBond()) + "元");
        }

        if (mshipperManagement) {
            if (manageItemData.getGoodsPattern() == 1) {
                mBinding.rlSendMode.setVisibility(View.VISIBLE);
                mBinding.tvSendMode.setText("临时货源");
                mBinding.tvSendModeAx.setText("(单次货运，接单即终结)");
            } else if (manageItemData.getGoodsPattern() == 2) {
                mBinding.rlSendMode.setVisibility(View.VISIBLE);
                mBinding.tvSendMode.setText("长期货源");
                mBinding.tvSendModeAx.setText("(长期货运，司机可多次接单)");
            }
        }
    }

    //设置企业信息
    private void setbottomCompanyInfo(GoodsSourceDetail manageItemData) {
        mBinding.bottomCompay.callPhonePerson.setVisibility(View.GONE);
        mBinding.bottomCompay.callPhone.setVisibility(View.GONE);
        RequestOptions options = new RequestOptions()
                .dontAnimate()
                .placeholder(manageItemData.getTerminal() != null ? manageItemData.getTerminal() == 3 ? R.drawable.geren_person_touxiang : manageItemData.getTerminal() == 1 ? R.mipmap.goods_list_company_icon : R.drawable.goods_detail_touxiang : R.drawable.goods_detail_touxiang)
                .error(manageItemData.getTerminal() != null ? manageItemData.getTerminal() == 3 ? R.drawable.geren_person_touxiang : manageItemData.getTerminal() == 1 ? R.mipmap.goods_list_company_icon : R.drawable.goods_detail_touxiang : R.drawable.goods_detail_touxiang)
                .centerCrop();
        if (!isDestroyed()) {
            switch (manageItemData.getTerminal()) {
                case 1:
                    mBinding.bottomCompay.imgLogo.setOval(false);
                    mBinding.bottomCompay.imgLogo.setCornerRadius(4);
                    break;
                case 2:
                    //个人不展示
                case 3:
                    mBinding.bottomCompay.imgLogo.setOval(true);
                    break;
            }
            Glide.with(this).load(manageItemData.getLogo()).apply(options).into(mBinding.bottomCompay.imgLogo);
        }
        mBinding.bottomCompay.companyName.setText(manageItemData.getCompanyName());
        mBinding.bottomCompay.businessAddressText.setText(manageItemData.getCompanyAddress());
        mBinding.bottomCompay.enterpriseContactName.setText(manageItemData.getConnection());
        mBinding.bottomCompay.enterprisePhoneNumber.setText(manageItemData.getConnectionPhone());
        mBinding.bottomCompay.role.setVisibility(View.GONE);
        mBinding.bottomCompay.personRole.setVisibility(View.GONE);
        if (manageItemData.getTerminal() != null && manageItemData.getTerminal() == 3) {
            mBinding.headeCompay.callPhonePerson.setVisibility(View.VISIBLE);
            mBinding.bottomCompay.companyContacks.setVisibility(View.GONE);
            mBinding.bottomCompay.businessAddress.setText("联系电话：");
            mBinding.bottomCompay.businessAddressText.setText(manageItemData.getConnectionPhone());
            mBinding.bottomCompay.companyName.setText(manageItemData.getConnection());
        }
        mBinding.bottomCompay.tvIcon.setVisibility(View.GONE);
    }

    /**
     * 点击事件
     */
    @Override
    public void onMultiClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.on_the_shelf:
                dropOffDialog();
                break;
            case R.id.editing:
                PublishCargoActivity.start(getActivity(), mGoodsSourceDetail.getNo(), true);
                break;
            case R.id.delete:
                showDelDialog();
                break;

            case R.id.tv_report:
                if (!isLogin()) {
                    VerificationCodeLoginActivity.start(GeneralPrjDetailActivity.this);
                    return;
                }
                flowLog=new LogisticsFlowLog(pageNameType,"货源联系人","button");
                showConsignorDialog(mGoodsSourceDetail.getConnectionPhone());
                break;
            case R.id.owner_phone_value:
            case R.id.tv_contact_bohao:
                if (!isLogin()) {
                    VerificationCodeLoginActivity.start(GeneralPrjDetailActivity.this);
                    return;
                }
                flowLog=new LogisticsFlowLog(pageNameType,"货源联系人","icon");
                showConsignorDialog(mGoodsSourceDetail.getConnectionPhone());
                //ReportActivity.start(this, sourceNo, "2");
                break;
            case R.id.tv_contact_service:
                if (!isLogin()) {
                    VerificationCodeLoginActivity.start(GeneralPrjDetailActivity.this);
                    return;
                }
                if (!contactsIsInSystem()) {
                    return;
                }
                if (mGoodsSourceDetail != null) {
                    // TODO IM功能目前需要全部去掉，此处需要后期IM迁移完成后完善
                    if (!TextUtils.isEmpty(mGoodsSourceDetail.getContrctsImAccount())) {
                        buildSingleTalking(mGoodsSourceDetail.getContrctsImAccount());
                    } else {
                        if (!TextUtils.isEmpty(mGoodsSourceDetail.getImAccount())) {
                            buildSingleTalking(mGoodsSourceDetail.getImAccount());
                        }
                    }

                }
                break;
            case R.id.tv_inquire_by_letter:
                if (!isLogin()) {
                    VerificationCodeLoginActivity.start(GeneralPrjDetailActivity.this);
                    return;
                }
                //TODO 函询
                if (mGoodsSourceDetail ==null&&mGoodsSourceDetail.getPattern()==1) {
                    InquireByLetterActvity.start(this, 1, mGoodsSourceDetail.getConnection(), mGoodsSourceDetail.getUserNo(), mGoodsSourceDetail.getNo(), mGoodsSourceDetail.getCityName() + "\u3000" + mGoodsSourceDetail.getDistrictName() + "->" + mGoodsSourceDetail.getDestCityName() + "\u3000" + mGoodsSourceDetail.getDestCityName());
                } else {
                    InquireByLetterActvity.start(this, 0, mGoodsSourceDetail.getConnection(), mGoodsSourceDetail.getUserNo(), mGoodsSourceDetail.getNo(), mGoodsSourceDetail.getCityName() + "\u3000" + mGoodsSourceDetail.getDistrictName() + "->" + mGoodsSourceDetail.getDestCityName() + "\u3000" + mGoodsSourceDetail.getDestCityName());
                }
                break;
            case R.id.tv_commit:
                if (!isLogin()) {
                    VerificationCodeLoginActivity.start(GeneralPrjDetailActivity.this);
                    return;
                } else {
                    mModel.refresh(this);
                }
                break;
            case R.id.tv_take_orders:
                if (!isLogin()) {
                    VerificationCodeLoginActivity.start(GeneralPrjDetailActivity.this);
                    return;
                } else {
                    LogisticsPermissionHelper.getInstance().checkPermissionToOrderQuote(this, mGoodsSourceDetail.getNo(), mGoodsSourceDetail.getTerminal(), mGoodsSourceDetail.getMessagePrice());
                }
                break;
            case R.id.consignor_ggt_ditu_icon:
                company = mGoodsSourceDetail.getTerminal() == 3 ? "" : mGoodsSourceDetail.getCompanyName();
                //TODO 地址详情点击事件
                GoodsSourcePlace place;
                if (mGoodsSourceDetail.getPlace() == null) {
                    place = EntityConversion.modelA2B(mGoodsSourceDetail, GoodsSourcePlace.class);
                } else {
                    place = mGoodsSourceDetail.getPlace();
                }
                switch (mGoodsSourceDetail.getHandleTypeNo()) {
                    case "11":
                        HandleAdressActivity.start(GeneralPrjDetailActivity.this, mGoodsSourceDetail.getConnection(), company, mGoodsSourceDetail.getConnectionPhone(), mGoodsSourceDetail.getImAccount(), place, 0, "11");
                        break;
                    case "12":
                        HandleAdressActivity.start(GeneralPrjDetailActivity.this, mGoodsSourceDetail.getConnection(), company, mGoodsSourceDetail.getConnectionPhone(), mGoodsSourceDetail.getImAccount(), place, 0, "12");
                        break;
                    case "21":
                        HandleAdressActivity.start(GeneralPrjDetailActivity.this, mGoodsSourceDetail.getConnection(), company, mGoodsSourceDetail.getConnectionPhone(), mGoodsSourceDetail.getImAccount(), place, 0, "21");
                        break;
                    case "22":
                        HandleAdressActivity.start(GeneralPrjDetailActivity.this, mGoodsSourceDetail.getConnection(), company, mGoodsSourceDetail.getConnectionPhone(), mGoodsSourceDetail.getImAccount(), place, 0, "22");
                        break;
                }
                break;
            case R.id.ggt_ditu_icon:
                company = mGoodsSourceDetail.getTerminal() == 3 ? "" : mGoodsSourceDetail.getCompanyName();
                //TODO 地址详情点击事件
                GoodsSourcePlace place1;
                if (mGoodsSourceDetail.getPlace() == null) {
                    place1 = EntityConversion.modelA2B(mGoodsSourceDetail, GoodsSourcePlace.class);
                } else {
                    place1 = mGoodsSourceDetail.getPlace();
                }
                switch (mGoodsSourceDetail.getHandleTypeNo()) {
                    case "11":
                        HandleAdressActivity.start(GeneralPrjDetailActivity.this, mGoodsSourceDetail.getConnection(), company, mGoodsSourceDetail.getConnectionPhone(), mGoodsSourceDetail.getImAccount(), place1, 1, "11");
                        break;
                    case "12":
                        HandleAdressActivity.start(GeneralPrjDetailActivity.this, mGoodsSourceDetail.getConnection(), company, mGoodsSourceDetail.getConnectionPhone(), mGoodsSourceDetail.getImAccount(), place1, 2, "12");
                        break;
                    case "21":
                        HandleAdressActivity.start(GeneralPrjDetailActivity.this, mGoodsSourceDetail.getConnection(), company, mGoodsSourceDetail.getConnectionPhone(), mGoodsSourceDetail.getImAccount(), place1, 2, "21");
                        break;
                    case "22":
                        HandleAdressActivity.start(GeneralPrjDetailActivity.this, mGoodsSourceDetail.getConnection(), company, mGoodsSourceDetail.getConnectionPhone(), mGoodsSourceDetail.getImAccount(), place1, 3, "22");
                        break;
                }
                break;
            case R.id.offerNum:
                if (!isLogin()) {
                    VerificationCodeLoginActivity.start(GeneralPrjDetailActivity.this);
                    return;
                }
                OfferListGeneralGoodsActivity.start(getActivity(), mGoodsSourceDetail.getNo(), mGoodsSourceDetail.getPattern() != null && mGoodsSourceDetail.getPattern() != 1);
                break;
            case R.id.manager_status_figure_operation:
            case R.id.tv_figure_operation:
            case R.id.quotation_reason:
                if (!isLogin()) {
                    VerificationCodeLoginActivity.start(GeneralPrjDetailActivity.this);
                    return;
                }
                //判断是否货主身份
                if ((UserOperating.getInstance().isCompany() && UserOperating.getInstance().getTeamOrCompanyNameNo().equals(mGoodsSourceDetail.getCompanyNo()) && mGoodsSourceDetail.getTerminal() == 1)
                        || (UserOperating.getInstance().isTeam() && UserOperating.getInstance().getTeamOrCompanyNameNo().equals(mGoodsSourceDetail.getTeamNo()) && mGoodsSourceDetail.getTerminal() == 2)
                        || (UserOperating.getInstance().isPersonal() && UserOperating.getInstance().getUserNo().equals(mGoodsSourceDetail.getUserNo()) && mGoodsSourceDetail.getTerminal() == 3)) {
                    OfferOwnerActivity.start(this, mGoodsSourceDetail.getNo());
                } else {
                    OfferThirdActivity.start(this, mGoodsSourceDetail.getNo());
                }
                break;
            case R.id.tv_invitation:
                LogisticsPermissionHelper.getInstance().checkPermissionToInvite(this, mGoodsSourceDetail.getCompanyNo(), mGoodsSourceDetail.getTerminal());
                break;
            case R.id.load_phone_value:
                if (!isLogin()) {
                    VerificationCodeLoginActivity.start(GeneralPrjDetailActivity.this);
                    return;
                }
                flowLog=new LogisticsFlowLog(pageNameType,"装车对接人","icon");
                showConsignorDialog(mGoodsSourceDetail.getSendPhone());
                break;
            case R.id.unload_phone_value:
                if (!isLogin()) {
                    VerificationCodeLoginActivity.start(GeneralPrjDetailActivity.this);
                    return;
                }
                flowLog=new LogisticsFlowLog(pageNameType,"卸车对接人","icon");
                showConsignorDialog(mGoodsSourceDetail.getReceptionPhone());
                break;
            case R.id.share_image_view://分享
                if (mGoodsSourceDetail == null)
                    return;
                StringBuilder title = new StringBuilder();
                StringBuilder content = new StringBuilder();
                if (!TextUtils.isEmpty(mGoodsSourceDetail.getCityName())) {
                    title.append(mGoodsSourceDetail.getCityName());
                }
                if (!TextUtils.isEmpty(mGoodsSourceDetail.getDistrictName())) {
                    title.append(" ").append(mGoodsSourceDetail.getDistrictName());
                }
                title.append(" → ");
                if (!TextUtils.isEmpty(mGoodsSourceDetail.getDestCityName())) {
                    title.append(mGoodsSourceDetail.getDestCityName());
                }
                if (!TextUtils.isEmpty(mGoodsSourceDetail.getDestDistrictName())) {
                    title.append(" ").append(mGoodsSourceDetail.getDestDistrictName());
                }
                String wightStr = (TextUtils.isEmpty(mGoodsSourceDetail.getWeight()) || TextUtils.isDigitsOnly(mGoodsSourceDetail.getWeight()) ? "" : "/" + NumberUtil.trim0(mGoodsSourceDetail.getWeight()) + "吨");
                String volumeStr = TextUtils.isEmpty(mGoodsSourceDetail.getVolume()) ? "" : "/" + NumberUtil.trim0(mGoodsSourceDetail.getVolume()) + "方";
                content.append(CargoConstant.getLastGoodsTypeName(mGoodsSourceDetail.getGoodsTypesName(), mGoodsSourceDetail.getSecondGoodsTypeName())).append(wightStr).append(volumeStr);

                ShareLogisticsActivity.start(GeneralPrjDetailActivity.this, title.toString(), content.toString(), "", mGoodsSourceDetail.getShareUrl(), false);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LogisticsEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_CARGO_RELEASE_SUCCESS) {
            mBinding.contnetLayout.setVisibility(View.GONE);
            Request();
        } else if (event.getMessageTag() == EventConstant.EVENT_CARINFO_COMMIT) {//接收新增车辆提交事件
            UserOperating.getInstance().refresh();
        } else if (event.getMessageTag() == EventConstant.EVENT_REFRESH_MY_OFFER_PAGE) {
            if ("ture".equals(event.getMessageData())) {
                finish();
            } else {
                mBinding.contnetLayout.setVisibility(View.GONE);
                Request();
            }
        }
        if (event.getMessageTag() == EventConstant.EVENT_REFRESH_PAY_GOODS) {
            Request();
        }
    }

    //开通个人资金账户后判断
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getCode(MessageEvent event) {
        switch (event.code) {
            case EventConstant.EVENT_REFRESH:
                UserOperating.getInstance().refresh();
                break;
            case EventConstant
                    .EVENT_REFRESH_GOODS:
                String orderNo = (String) event.data;
                this.orderNo = orderNo;
                mBinding.contnetLayout.setVisibility(View.GONE);
                // Request();
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReFresh(ZMessageEvent event) {
        if (event.code == Constants.EVENT_REFRESH) {//放弃支付运费关闭
            if (!TextUtils.isEmpty(orderNo)) {
                mModel.unfreeze(this, sourceNo, orderNo);
            }
            mModel.ldUnfreeze.observe(this, a -> {
                mBinding.contnetLayout.setVisibility(View.GONE);
                mBinding.frontBottom.setVisibility(View.GONE);
                Request();
            });
        } else if (event.code == Constants.EVENT_REFRESH_ORDER || event.code == Constants.EVENT_PAY_SUCCESS) {
            orderNo="";
            Request();
        }

    }

    private void Request() {
        mModel.GeneralPrjDetailModel(this, sourceNo, mshipperManagement ? 1 : null);
        mModel.GoodsSourceDetailData.observe(this, list -> {
            mBinding.frontBottom.setVisibility(mshipperManagement ? View.GONE : View.VISIBLE);
            mBinding.contnetLayout.setVisibility(View.VISIBLE);
            setDetail(list);
        });
    }

    /**
     * 状态判断
     * 0 是普通货源详情（显示接单tag）反之显示竞价时间标识 1 已完成（普通货源）2 竞价货源截止
     * （待接单、已成交、已失效）普通货源详情/（竞价中、已结束、已失效）竞价详情【大厅/货主】
     */
    private void setStateInfo(GoodsSourceDetail info) {
        if (info.getState() == null)
            return;
        switch (info.getState()) {
            case 0://接单中/竞价中
                setReuseState(info, 0);
                break;
            case 1:
                mBinding.quotationBottom.setVisibility(View.GONE);
                    mBinding.offerNum.setVisibility(View.GONE);
                    setLoadTimeBing(info);
                    mBinding.managerStatus.setText("已成交");
                    mBinding.tvCommit.setText("接单");
                    mBinding.tvCommit.setEnabled(false);
                    mBinding.tvCommit.setAlpha((float) 0.6);
                    mBinding.tvTakeOrders.setEnabled(false);
                    mBinding.tvTakeOrders.setAlpha((float) 0.6);
                    if (mshipperManagement) {
                        mBinding.goodsLayout.setVisibility(View.GONE);
                        mBinding.bottomLayout.setVisibility(View.GONE);
                        setOffNumShow(info, 1);
                        if (!TextUtils.isEmpty(info.getOrderNo())) {
                            mBinding.goodsLayout.setVisibility(View.GONE);
                            mBinding.bottomLayout.setVisibility(View.GONE);
                        }

                        mBinding.onTheShelf.setText("1".equals(info.getStatus()) ? "下架" : "上架");
                        mBinding.onTheShelf.setTextColor(Color.parseColor("#969696"));
                        mBinding.onTheShelf.setEnabled(false);
                    }
                break;
            case 2:
                mBinding.quotationBottom.setVisibility(View.GONE);
                setLoadTimeBing(info);
                mBinding.managerStatus.setText("已失效");
                if (mshipperManagement) {
                    setOffNumShow(info, 2);
                    mBinding.onTheShelf.setText("1".equals(info.getStatus()) ? "下架" : "上架");
                    mBinding.onTheShelf.setTextColor(Color.parseColor("#969696"));
                    mBinding.onTheShelf.setEnabled(false);
                    mBinding.editing.setEnabled(false);
                    mBinding.editing.setTextColor(Color.parseColor("#969696"));
                }
                break;
            case 3:
                mBinding.quotationBottom.setVisibility(View.GONE);
                setReuseState(info, 3);
                break;
            case 4:
                mBinding.quotationBottom.setVisibility(View.GONE);
                setReuseState(info, 4);
                break;
        }
        if (info.getStatus() != null && info.getStatus().equals("0")) {
            mBinding.quotationBottom.setVisibility(View.GONE);
            mBinding.managerStatus.setText("已下架");
            mBinding.tvCommit.setEnabled(false);
            mBinding.tvCommit.setAlpha((float) 0.6);
            mBinding.tvTakeOrders.setEnabled(false);
            mBinding.tvTakeOrders.setAlpha((float) 0.6);
        }
        if (mshipperManagement) {
            mBinding.showQuotationUnit.setVisibility(View.VISIBLE);
            mBinding.offerNum.setVisibility(View.VISIBLE);
            if (info.getOfferNum() == null || info.getOfferNum() == 0) {
                mBinding.offerNum.setText("0" + "个车主报价");
            } else {
                if (info.getOfferNum() > 99) {
                    mBinding.offerNum.setText("99+个车主报价");
                } else {
                    mBinding.offerNum.setText(info.getOfferNum() + "个车主报价");
                }
            }
        } else {
            mBinding.offerNum.setVisibility(View.GONE);
        }
    }

    private void setEndState(GoodsSourceDetail info, int state) {
        setLoadTimeBing(info);
        mBinding.managerStatus.setText("已结束");
        mBinding.tvCommit.setText("接单");
        if (mshipperManagement) {
            mBinding.onTheShelf.setText("1".equals(info.getStatus()) ? "下架" : "上架");
            setOffNumShow(info, state);
            mBinding.onTheShelf.setTextColor(Color.parseColor("#969696"));
            mBinding.onTheShelf.setEnabled(false);
        }else {

        }
    }

    public void setReuseState(GoodsSourceDetail info, int state) {
        if (state == 3) {
            mBinding.managerStatus.setText("付款中");
            mBinding.tvCommit.setEnabled(false);
            mBinding.tvCommit.setAlpha((float) 0.6);
            mBinding.tvTakeOrders.setEnabled(false);
            mBinding.tvTakeOrders.setAlpha((float) 0.6);
            if (mshipperManagement) {
                mBinding.onTheShelf.setEnabled(false);
                mBinding.editing.setEnabled(false);
                mBinding.delete.setEnabled(false);
                mBinding.onTheShelf.setTextColor(Color.parseColor("#969696"));
                mBinding.editing.setTextColor(Color.parseColor("#969696"));
                mBinding.delete.setTextColor(Color.parseColor("#969696"));
            }
        } else if (state == 4) {
            mBinding.managerStatus.setText("已下架");
            mBinding.tvCommit.setEnabled(false);
            mBinding.tvCommit.setAlpha((float) 0.6);
            mBinding.tvTakeOrders.setEnabled(false);
            mBinding.tvTakeOrders.setAlpha((float) 0.6);
        } else {
            mBinding.managerStatus.setText("待接单");
            mBinding.tvCommit.setEnabled(true);
            mBinding.tvCommit.setAlpha((float) 1);
            mBinding.tvTakeOrders.setEnabled(true);
            mBinding.tvTakeOrders.setAlpha((float) 1);
        }
        //普通货源
        setLoadTimeBing(info);
        mBinding.tvCommit.setText("接单");
        if (mshipperManagement) {
            mBinding.onTheShelf.setText("1".equals(info.getStatus()) ? "下架" : "上架");
        } else {
            mBinding.cutoffTime.setVisibility(View.VISIBLE);
        }
        if (isLogin()){
            mBinding.quotationBottom.setVisibility( TextUtils.isEmpty(info.getLastFreight()) ? View.GONE : View.VISIBLE);
        }
        mBinding.quotationBottom.setVisibility(UserOperating.getInstance().isPersonal() ? View.GONE : TextUtils.isEmpty(info.getLastFreight()) ? View.GONE : View.VISIBLE);
        mBinding.quotationBottom.setVisibility(TextUtils.isEmpty(info.getLastFreight())?View.GONE:View.VISIBLE);
        mBinding.latestQuotation.setText(info.getLastFreight());
    }

    public void setLoadTimeBing(GoodsSourceDetail info) {
        mBinding.ingRl.setVisibility(View.VISIBLE);
//        if (mshipperManagement) {
//            mBinding.goodsUpdateTime.setText("货源更新时间    " + DateUtils.stampToMinute("" + info.getUpdateTime()));
//            if (info.getEndTime() == null) {
//                mBinding.goodsEndTime.setVisibility(View.GONE);
//            } else {
//                mBinding.goodsEndTime.setText("报价截止时间    " + DateUtils.stampToMinute("" + info.getEndTime()));
//            }
//        } else {
            mBinding.goodsEndTime.setVisibility(View.GONE);
//        }
        if (info.getShipmentTime() == null) {
            mBinding.goodsUpdateTime.setVisibility(View.GONE);
            return;
        }
        if (mlobbyselect == 1) {
//            if (mshipperManagement) {
//                mBinding.goodsUpdateTime.setText("货源更新时间    " + DateUtils.stampToMinute("" + info.getUpdateTime()));
//            } else {
                mBinding.goodsUpdateTime.setText("装货时间    " + (info.getShipmentTime() == null ? "" : DateUtils.stampToDay5(info.getShipmentTime() + "")) + (info.getShipmentEndTime() == null ? "" : "-" + DateUtils.stampToDay5(info.getShipmentEndTime() + "")));
//            }
        } else {
            if (CargoConstant.stampFormatLoadInput(info.getShipmentTime(), info.getStartPointTime()).equals(CargoConstant.stampFormatLoadInput(info.getShipmentEndTime(), info.getEndPointTime()))) {
                mBinding.goodsUpdateTime.setText("装货时间    " + CargoConstant.stampFormatLoadInput(info.getShipmentTime(), info.getStartPointTime()));
            } else {
                mBinding.goodsUpdateTime.setText("装货时间    " + CargoConstant.stampFormatLoadInput(info.getShipmentTime(), info.getStartPointTime()) + (TextUtils.isEmpty(CargoConstant.stampFormatLoadInput(info.getShipmentEndTime(), info.getEndPointTime())) ? "" : "-") + CargoConstant.stampFormatLoadInput(info.getShipmentEndTime(), info.getEndPointTime()));
            }
        }
    }

    private void setShow() {
        if (mshipperManagement) {//货源管理
            mBinding.ingRl.setVisibility(View.VISIBLE);
            mBinding.bottomLayout.setVisibility(View.VISIBLE);
            mBinding.bottomCompay.rlInfo.setVisibility(View.VISIBLE);
            mBinding.goodsLayout.setVisibility(View.VISIBLE);
        } else {
            mBinding.goodsLayout.setVisibility(View.GONE);
            mBinding.bottomLayout.setVisibility(View.VISIBLE);
            mBinding.headLlCompay.setVisibility(View.VISIBLE);
            mBinding.headeCompay.rlInfo.setVisibility(View.VISIBLE);
        }
        if ("下架".equals(mBinding.onTheShelf.getText())) {
            mBinding.onTheShelf.setTextColor(Color.parseColor("#969696"));
        } else {
            mBinding.onTheShelf.setTextColor(Color.parseColor("#333333"));
        }
    }

    private boolean isLogin() {
        return !TextUtils.isEmpty(SharedPreferenceUtil.getInstance().
                getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""));
    }

    private boolean contactsIsInSystem() {
        if (mGoodsSourceDetail == null) {
            return false;
        }
        return !TextUtils.isEmpty(mGoodsSourceDetail.getContrctsImAccount()) || !TextUtils.isEmpty(mGoodsSourceDetail.getImAccount());

    }

    /**
     * 删除提示
     */
    private void showDelDialog() {
        new RoundDialog.Builder(getActivity())
                .title("温馨提示")
                .content("确定删除吗？")
                .confirmCallback(v -> mModel.deleteGoods(this, String.valueOf(mGoodsSourceDetail.getId())))
                .show();
    }

    /**
     * 上下架提示
     */
    private void dropOffDialog() {
        new RoundDialog.Builder(getActivity())
                .title("1".equals(mGoodsSourceDetail.getStatus())
                        ? "下架"
                        : "上架")
                .content("1".equals(mGoodsSourceDetail.getStatus())
                        ? "下架后前台不展示此信息，\n确认下架吗？"
                        : "上架后前台展示此信息，确认上架吗？")
                .confirmCallback(v -> mModel.downAndUpGoods(this, mGoodsSourceDetail.getNo()))
                .show();
    }

    public void deleteInfoSuccess() {
        EventBus.getDefault().post(LogisticsEvent.make(EventConstant.EVENT_GOOD_DETAIL_DELETE, null));
        finish();
    }

    public void downAndUpGoods() {
        mModel.GeneralPrjDetailModel(this, sourceNo, mshipperManagement ? 1 : null);
        mModel.GoodsSourceDetailData.observe(this, list -> {
            setDetail(list);
        });
        EventBus.getDefault().post(LogisticsEvent.make(EventConstant.EVENT_GOOD_DETAIL_UP_AND_DOWN_SUCCESS, null));
    }

    @Override
    public void onItemClick(GoodsSourcePlace place, String i, int position) {
        //TODO 去地图查看界面
    }

    public void setOffNumShow(GoodsSourceDetail info, int state) {
        if (info.getOfferNum() == null || info.getOfferNum() == 0) {
            mBinding.offerNum.setVisibility(View.VISIBLE);
            mBinding.offerNum.setText("0" + "个车主报价");
            mBinding.goodsLayout.setVisibility(View.VISIBLE);
            mBinding.bottomLayout.setVisibility(View.VISIBLE);
        } else {
            mBinding.offerNum.setVisibility(View.VISIBLE);
            mBinding.offerNum.setText(info.getOfferNum() + "人报价");
            mBinding.goodsLayout.setVisibility(View.GONE);
            mBinding.bottomLayout.setVisibility(View.GONE);
        }
    }

    //打电话弹窗
    public void showConsignorDialog(String phone) {
        new RoundDialog.Builder(this)
                .title("确认呼叫")
                .content(phone)
                .cancel("取消")
                .confirm("确定")
                .contentGravity(Gravity.CENTER)
                .cancelable(false)
                .confirmCallback(new RoundDialog.SingleCallback() {
                    @Override
                    public void onClick(View v) {
                        if(flowLog!=null) {
                            LogisticsLogUtils.getInstance().logisticsFlow(flowLog);
                        }
                        callPhone(phone);
                    }
                })
                .show();
    }

    //功能正在开发
    public void showdevelopmentDialog() {
        new RoundDialog.Builder(this)
                .title("提示")
                .content("该功能正在开发中")
                .singleBtn(true)
                .confirm("确定")
                .contentGravity(Gravity.CENTER)
                .cancelable(false)
                .show();
    }

    //判断团队或企业是否开通资金账户
    public void getGoodsOwnerInfo(UserAccount userAccount) {
        if (mGoodsSourceDetail.getTerminal() == null) {
            return;
        }
        if (!userAccount.isIsDriverOpenFundsAccount() && mGoodsSourceDetail.getTerminal() == 2) {//货源货主团队没开通资金账户
            new RoundDialog.Builder(this)
                    .title("提示")
                    .content(mlobbyselect == 1 ? "货主未开通收款账户，无法报价" : "货主未开通收款账户，无法接单")
                    .singleBtn(true)
                    .confirm("确定")
                    .contentGravity(Gravity.CENTER)
                    .cancelable(false)
                    .show();
        } else if (!userAccount.isIsOpenFundsAccount() && mGoodsSourceDetail.getTerminal() == 1) {//货源货主企业没开通资金账户
            new RoundDialog.Builder(this)
                    .title("提示")
                    .content(mlobbyselect == 1 ? "货主未开通收款账户，无法报价" : "货主未开通收款账户，无法接单")
                    .singleBtn(true)
                    .confirm("确定")
                    .contentGravity(Gravity.CENTER)
                    .cancelable(false)
                    .show();
        } else if (!userAccount.isIsDriverOpenFundsAccount() && mGoodsSourceDetail.getTerminal() == 3) {//个人货源资金账户
            new RoundDialog.Builder(this)
                    .title("提示")
                    .content(mlobbyselect == 1 ? "货主未开通收款账户，无法报价" : "货主未开通收款账户，无法接单")
                    .singleBtn(true)
                    .confirm("确定")
                    .contentGravity(Gravity.CENTER)
                    .cancelable(false)
                    .show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    private void fieldDisplay(GoodsSourceDetail info) {
        if (TextUtils.isEmpty(info.getInvoiceWords())) {
            mBinding.gtdInvoiceRequirement.setVisibility(View.GONE);
            mBinding.gtdInvoiceRequirementText.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(info.getPayType())) {
            mBinding.gtdPaymentMethod.setVisibility(View.GONE);
            mBinding.gtdPaymentMethodText.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(info.getPerformanceBond())) {
            mBinding.splin3Line.setVisibility(View.GONE);
            mBinding.rlSecurityDeposit.setVisibility(View.GONE);
        }
    }
}
